package com.door.erp.dao.hibernate;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Repository;

import com.door.erp.dao.OrderDao;
import com.door.erp.dao.base.GenericDaoImpl;
import com.door.erp.enums.OrderStatus;
import com.door.erp.model.Orders;
import com.door.erp.pager.Pager;

@Repository(value = "orderDao")
public class OrderDaoImp extends GenericDaoImpl<Orders, Long> implements
		OrderDao {

	@Override
	protected Class<Orders> getPersistentClass() {
		return Orders.class;
	}
	
	public List<Orders> listOrders(final int startRow,
			final int pageSize){
		return this.getHibernateTemplate().execute(new HibernateCallback<List<Orders>>() {
			@Override
			public List<Orders> doInHibernate(Session session) throws HibernateException,
					SQLException {
				Criteria criteria = session.createCriteria(getPersistentClass());
				criteria.add(Restrictions.ne("status", OrderStatus.SAVE));
				criteria.setFirstResult(startRow);
				criteria.setMaxResults(pageSize);
				return (List<Orders>) criteria.list();
			}
		});
	}
	
	public int listOrdersCount(){
		return this.getHibernateTemplate().execute(new HibernateCallback<Integer>() {
			@Override
			public Integer doInHibernate(Session session) throws HibernateException,
					SQLException {
				Criteria criteria = session.createCriteria(getPersistentClass());
				criteria.add(Restrictions.ne("status", OrderStatus.SAVE));
				long count = (Long)criteria.setProjection(Projections.rowCount()).uniqueResult();
				return (int)count;
			}
		});
	}

	@Override
	public List<Orders> listOrders(final Map<String, Object> params, final int startRow,
			final int pageSize) {
		return this.getHibernateTemplate().execute(new HibernateCallback<List<Orders>>() {
			@Override
			public List<Orders> doInHibernate(Session session) throws HibernateException,
					SQLException {
				Criteria criteria = session.createCriteria(getPersistentClass());
				criteria.add(Restrictions.ne("status", OrderStatus.SAVE));
				if(params!=null){
					criteria.add(Restrictions.allEq(params));
				}
				criteria.setFirstResult(startRow);
				criteria.setMaxResults(pageSize);
				return (List<Orders>) criteria.list();
			}
		});
	}

	@Override
	public int listOrdersCount(final Map<String, Object> params) {
		return this.getHibernateTemplate().execute(new HibernateCallback<Integer>() {
			@Override
			public Integer doInHibernate(Session session) throws HibernateException,
					SQLException {
				Criteria criteria = session.createCriteria(getPersistentClass());
				criteria.add(Restrictions.ne("status", OrderStatus.SAVE));
				if(params !=null){
					criteria.add(Restrictions.allEq(params));
				}
				long count = (Long)criteria.setProjection(Projections.rowCount()).uniqueResult();
				return (int)count;
			}
		});
	}
	
	public List<Orders> getOrdersByExport(final Date beginTime, final Date endTime, final OrderStatus orderStatus) {
		return this.getHibernateTemplate().execute(new HibernateCallback<List<Orders>>() {
			@Override
			public List<Orders> doInHibernate(Session session) throws HibernateException,
					SQLException {
				Criteria criteria = session.createCriteria(getPersistentClass());
				criteria.add(Restrictions.eq("status", orderStatus));
				criteria.add(Restrictions.between("createTime", beginTime, endTime));
				return (List<Orders>) criteria.list();
			}
		});
	}

}
